﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Polymod
{
    internal class FormulaInvoker : IInvoker
    {
        Func<IModel, object> _compiledFormula;
        private Type _returnType;
        private string _name;

        public object GetValue(IModel target)
        {
            return _compiledFormula(target);
        }

        public void SetValue(IModel target, object value)
        {
            //NOTE: Cannot set the value of a formula;
            throw new NotSupportedException();
        }

        public Type ReturnType
        {
            get { return _returnType; }
        }

        public FormulaInvoker(Func<IModel, object> compiledFormula, string name, Type returnType)
        {
            if (name == null) throw new System.ArgumentNullException("name");
            _compiledFormula = compiledFormula;
            _returnType = returnType;
            _name = name;
        }

        public IInvoker Clone()
        {
            var result = new FormulaInvoker(_compiledFormula, _name, _returnType);
            return result;
        }


        public bool IsReadonly
        {
            get { return true; }
        }

        /// <summary>
        /// Gets Name
        /// </summary>
        public string Name
        {
            get { return _name; }
        }
    }
}
